<?php

namespace Home\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Home\Model\Benhnhan;
use Home\Form\BenhnhanForm;
use Zend\Db\Sql\Select;
use Zend\Paginator\Paginator;
use Zend\Paginator\Adapter\Iterator as paginatorIterator;
use Zend\Session\Container;

class BenhnhanController extends AbstractActionController {

    protected $benhnhanTable;
	public $menuTable;
	public $tintucTable;
	public $onlineTable;
	public $lienketTable;
	public $counterTable;
	public $visitTable;
	public $thongtinTable;

	
	public function counter(){
		$sid = session_id();
		$CurrentTime = time();
		$TimeOut = $CurrentTime - 300;
		$this->getOnlineTable()->check_exists($sid,$CurrentTime,$TimeOut);
		$select = new Select();
		$online = $this->getOnlineTable()->fetchAll()->count();
		$this->getEvent()->getViewModel()->online = $online;
		////
		$today = date('d');
		$thang = date('m');
		$nam = date('Y');
		$ip = $this->getRequest()->getServer('REMOTE_ADDR');
		if($this->getVisitTable()->check_date($today,$thang,$nam))
		{
			if($this->getVisitTable()->check_ip($ip,$today,$thang,$nam))
			{
				if($sid!=$this->getVisitTable()->check_ip($ip,$today,$thang,$nam)->session)
				{
					$this->getVisitTable()->updateVisit($ip,$sid,$today,$thang,$nam);
					$this->getCounterTable()->updateLuotxem($today,$thang,$nam);
				}
			}
			else
			{
				$this->getVisitTable()->insertVisit($ip,$sid,$today,$thang,$nam);
				$this->getCounterTable()->updateLuongtruycap($today,$thang,$nam);
			}
		}
		else
		{
			$this->getVisitTable()->deleteVisit();
			$this->getVisitTable()->insertVisit($ip,$sid,$today,$thang,$nam);
			$this->getCounterTable()->insertCounter($today,$thang,$nam);
		}
		//$this->getCounterTable()->check_exists($today,$thang,$nam);
		
		$select = new Select();
		$tongtruycap = $this->getCounterTable()->sumCounter();
		
		$this->getEvent()->getViewModel()->tongtruycap = $tongtruycap->luongtruycap;
		/////// \Zend\Debug\Debug::dump($tongtruycap->luongtruycap);
		
		$select = new Select();
		$thongtin = $this->getThongtinTable()->fetchAll();
		
		$this->getEvent()->getViewModel()->banner = $thongtin->banner;
		$this->getEvent()->getViewModel()->footer = $thongtin->footer;
		$this->getEvent()->getViewModel()->yahoo = $thongtin->yahoo;
		$this->getEvent()->getViewModel()->skype = $thongtin->skype;
	}
    public function indexAction() {
		$module = "home";
		$controller = $this->params('controller');
		$action = $this->params('action');
		
		$sessionModule = new Container('module');
		$sessionController = new Container('controller');
		$sessionAction = new Container('action');
		$sessionModule->lastModule = $module;
		$sessionController->lastController = $controller;
		$sessionAction->lastAction = $action;
		
		$select = new Select();
		
		//// domain
		$domain = "/truonghoc";
		$this->getEvent()->getViewModel()->domain = $domain;
		///////
		$this->getEvent()->getViewModel()->chuyenmuc = "benhnhan";

        $danhmuc = $this->getMenuTable()->fetchAllDanhmuc();
        $this->getEvent()->getViewModel()->danhmuc = $danhmuc;

		$canbiet = $this->getMenuTable()->fetchAllCanbiet();
        $this->getEvent()->getViewModel()->canbiet = $canbiet;
		
		$menuchinh = $this->getMenuTable()->fetchAllMenuchinh();
        $this->getEvent()->getViewModel()->menuchinh = $menuchinh;
		$this->getEvent()->getViewModel()->menuchinh1 = $menuchinh;
		
		$select = new Select();
		$noibat = $this->getTintucTable()->fetchAll($select->where(array('tinnoibat' => 'Nổi bật','trangthai' => 'Hiển thị'))->order('id desc')->limit(5));
		$this->getEvent()->getViewModel()->noibat = $noibat;
		
		/////
		$select = new Select();
		$moinhat = $this->getTintucTable()->fetchAll($select->where(array('trangthai' => 'Hiển thị'))->order('id desc')->limit(5));
		$this->getEvent()->getViewModel()->moinhat = $moinhat;
		
		/////
		/////
		$select = new Select();
		$quantam = $this->getTintucTable()->fetchAll($select->where(array('trangthai' => 'Hiển thị'))->order('soluotxem desc')->limit(5));
		$this->getEvent()->getViewModel()->quantam = $quantam;
		
		/////
		/////
		$select = new Select();
		$lienketwebsite = $this->getLienketTable()->fetchAll();
		$this->getEvent()->getViewModel()->lienketwebsite = $lienketwebsite;
		////
		$this->counter();
		
		
		$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        $form = new BenhnhanForm($dbAdapter);
        $form->get('submit')->setAttribute('value', 'Xem thông tin');
		
        $request = $this->getRequest();
        if ($request->isPost()) {
        	$order_by = $request->getPost('order_by');
        	$order = $request->getPost('order');
        	$page = $request->getPost('page');
        	if ($page === null) $page = 1;
        }
        else{
        	$order_by = $this->params()->fromRoute('order_by') ?
        	$this->params()->fromRoute('order_by') : 'id';
        	$order = $this->params()->fromRoute('order') ?
        	$this->params()->fromRoute('order') : Select::ORDER_ASCENDING;
        	$page = $this->params()->fromRoute('page') ? (int) $this->params()->fromRoute('page') : 1;
        }
        // biến flag để kiểm tra xem đã click nút tìm kiếm hay chưa, mặc định false
        $flag = "false";
        if ($request->isPost()) {
        	$flag = "true";
        	$cmt = $request->getPost('cmt');
        	$select = new Select();
        	$benhnhanInfo = $this->getBenhnhanTable()->fetchAll($select->where(array('cmt' => $cmt)));
//         	paginator
        	$itemsPerPage = 1;
        	$benhnhanInfo->current();
        	$paginator = new Paginator(new paginatorIterator($benhnhanInfo));
        	$paginator->setCurrentPageNumber($page)
        	          ->setItemCountPerPage($itemsPerPage);
        	return new ViewModel(array(
        		'benhnhaninfo' => $benhnhanInfo,
        		'paginator' => $paginator,
        		'form' => $form,	
        		'order_by' => $order_by,
        		'order' => $order,
        		'page' => $page,
        		'cmt' => $cmt,
        		'flag' => $flag,
        	)
        	);	
        }
		return array('form' => $form,'flag' => $flag);
    }

    public function addbenhnhanAction(){
    	$module = "home";
    	$controller = $this->params('controller');
    	$action = $this->params('action');
    	
    	$sessionModule = new Container('module');
    	$sessionController = new Container('controller');
    	$sessionAction = new Container('action');
    	$sessionModule->lastModule = $module;
    	$sessionController->lastController = $controller;
    	$sessionAction->lastAction = $action;
    	
    	$select = new Select();
    	
    	//// domain
    	$domain = "/truonghoc";
    	$this->getEvent()->getViewModel()->domain = $domain;
    	///////
    	$this->getEvent()->getViewModel()->chuyenmuc = "benhnhan";
    	
    	$danhmuc = $this->getMenuTable()->fetchAllDanhmuc();
    	$this->getEvent()->getViewModel()->danhmuc = $danhmuc;

    	$canbiet = $this->getMenuTable()->fetchAllCanbiet();
    	$this->getEvent()->getViewModel()->canbiet = $canbiet;
    	
    	$menuchinh = $this->getMenuTable()->fetchAllMenuchinh();
    	$this->getEvent()->getViewModel()->menuchinh = $menuchinh;
    	$this->getEvent()->getViewModel()->menuchinh1 = $menuchinh;
    	
    	$select = new Select();
    	$noibat = $this->getTintucTable()->fetchAll($select->where(array('tinnoibat' => 'Nổi bật','trangthai' => 'Hiển thị'))->order('id desc')->limit(5));
    	$this->getEvent()->getViewModel()->noibat = $noibat;
    	
    	/////
    	$select = new Select();
    	$moinhat = $this->getTintucTable()->fetchAll($select->where(array('trangthai' => 'Hiển thị'))->order('id desc')->limit(5));
    	$this->getEvent()->getViewModel()->moinhat = $moinhat;
    	
    	/////
    	/////
    	$select = new Select();
    	$quantam = $this->getTintucTable()->fetchAll($select->where(array('trangthai' => 'Hiển thị'))->order('soluotxem desc')->limit(5));
    	$this->getEvent()->getViewModel()->quantam = $quantam;
    	
    	/////
    	/////
    	$select = new Select();
    	$lienketwebsite = $this->getLienketTable()->fetchAll();
    	$this->getEvent()->getViewModel()->lienketwebsite = $lienketwebsite;
    	////
    	$this->counter();
    	
    	
    	$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
    	$form = new BenhnhanForm($dbAdapter);
    	$form->get('submit')->setAttribute('value', 'Lưu thông tin');
    	$request = $this->getRequest();
    	if ($request->isPost()) {
    		$benhnhan = new Benhnhan();
    		$form->setInputFilter($benhnhan->getInputFilter());
    		$form->setData($request->getPost());
//     		var_dump($form);
    		if ($form->isValid()) {
    			$benhnhan->exchangeArray($form->getData());
    			$this->getBenhnhanTable()->saveBenhnhan($benhnhan);
    			return $this->redirect()->toRoute('xembenhnhan');
    		} else {
    			print_r($form->getMessages());
    		}
    	}
    	return array('form' => $form);
    }

    public function editBenhnhanAction() {
    	$module = "home";
    	$controller = $this->params('controller');
    	$action = $this->params('action');
    	 
    	$sessionModule = new Container('module');
    	$sessionController = new Container('controller');
    	$sessionAction = new Container('action');
    	$sessionModule->lastModule = $module;
    	$sessionController->lastController = $controller;
    	$sessionAction->lastAction = $action;
    	 
    	$select = new Select();
    	 
    	//// domain
    	$domain = "/truonghoc";
    	$this->getEvent()->getViewModel()->domain = $domain;
    	///////
    	$this->getEvent()->getViewModel()->chuyenmuc = "benhnhan";
    	 
    	$danhmuc = $this->getMenuTable()->fetchAllDanhmuc();
    	$this->getEvent()->getViewModel()->danhmuc = $danhmuc;
    	
    	$canbiet = $this->getMenuTable()->fetchAllCanbiet();
    	$this->getEvent()->getViewModel()->canbiet = $canbiet;
    	 
    	$menuchinh = $this->getMenuTable()->fetchAllMenuchinh();
    	$this->getEvent()->getViewModel()->menuchinh = $menuchinh;
    	$this->getEvent()->getViewModel()->menuchinh1 = $menuchinh;
    	 
    	$select = new Select();
    	$noibat = $this->getTintucTable()->fetchAll($select->where(array('tinnoibat' => 'Nổi bật','trangthai' => 'Hiển thị'))->order('id desc')->limit(5));
    	$this->getEvent()->getViewModel()->noibat = $noibat;
    	 
    	/////
    	$select = new Select();
    	$moinhat = $this->getTintucTable()->fetchAll($select->where(array('trangthai' => 'Hiển thị'))->order('id desc')->limit(5));
    	$this->getEvent()->getViewModel()->moinhat = $moinhat;
    	 
    	/////
    	/////
    	$select = new Select();
    	$quantam = $this->getTintucTable()->fetchAll($select->where(array('trangthai' => 'Hiển thị'))->order('soluotxem desc')->limit(5));
    	$this->getEvent()->getViewModel()->quantam = $quantam;
    	 
    	/////
    	/////
    	$select = new Select();
    	$lienketwebsite = $this->getLienketTable()->fetchAll();
    	$this->getEvent()->getViewModel()->lienketwebsite = $lienketwebsite;
    	////
    	$this->counter();
    	
    	$id = (int) $this->params('id');
    	if (!$id) {
    		return $this->redirect()->toRoute('xembenhnhan',array('action' => 'index'));
    	}
    	$viewModel = new ViewModel();
    	$benhnhan = $this->getBenhnhanTable()->getBenhnhanById($id);
    	if (!$benhnhan) {
    		return $this->redirect()->toRoute('xembenhnhan');
    	}
    	$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
    	$form = new BenhnhanForm($dbAdapter);
    	$form->bind($benhnhan);
    	$form->get('submit')->setAttribute('value', 'Cập nhật');
    	
    	$request = $this->getRequest();
    	
    	if ($request->isPost()) {
    		$form->setData($request->getPost());
    		if ($form->isValid()) {
    			$this->getBenhnhanTable()->saveBenhnhan($benhnhan);
    			return $this->redirect()->toRoute('xembenhnhan');
    		}
    	}
    	return array(
    		'id' => $id,
    		'form' => $form,
    	);
    }
    public function getBenhnhanTable() {
        if (!$this->benhnhanTable) {
            $sm = $this->getServiceLocator();
            $this->benhnhanTable = $sm->get('Home\Model\BenhnhanTable');
        }
        return $this->benhnhanTable;
    }
	public function getMenuTable() {
        if (!$this->menuTable) {
            $sm = $this->getServiceLocator();
            $this->menuTable = $sm->get('Home\Model\MenuTable');
        }
        return $this->menuTable;
    }
	public function getTintucTable() {
        if (!$this->tintucTable) {
            $sm = $this->getServiceLocator();
            $this->tintucTable = $sm->get('Home\Model\TintucTable');
        }
        return $this->tintucTable;
    }
	public function getOnlineTable() {
        if (!$this->onlineTable) {
            $sm = $this->getServiceLocator();
            $this->onlineTable = $sm->get('Home\Model\OnlineTable');
        }
        return $this->onlineTable;
    }
	public function getLienketTable() {
        if (!$this->lienketTable) {
            $sm = $this->getServiceLocator();
            $this->lienketTable = $sm->get('Home\Model\LienketTable');
        }
        return $this->lienketTable;
    }
	public function getCounterTable() {
        if (!$this->counterTable) {
            $sm = $this->getServiceLocator();
            $this->counterTable = $sm->get('Home\Model\CounterTable');
        }
        return $this->counterTable;
    }
	public function getVisitTable() {
        if (!$this->visitTable) {
            $sm = $this->getServiceLocator();
            $this->visitTable = $sm->get('Home\Model\VisitTable');
        }
        return $this->visitTable;
    }
	public function getThongtinTable() {
        if (!$this->thongtinTable) {
            $sm = $this->getServiceLocator();
            $this->thongtinTable = $sm->get('Home\Model\ThongtinTable');
        }
        return $this->thongtinTable;
    }


}
